package disenio

import modelo.*
import org.uqbar.arena.bindings.NotNullObservable
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.layout.HorizontalLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.TextBox
import org.uqbar.arena.widgets.tables.Column
import org.uqbar.arena.widgets.tables.Table
import org.uqbar.arena.windows.Dialog
import org.uqbar.arena.windows.SimpleWindow
import org.uqbar.arena.windows.WindowOwner
import org.uqbar.arena.widgets.Selector
import org.uqbar.arena.bindings.ObservableProperty
import org.uqbar.arena.bindings.PropertyAdapter
import org.uqbar.commons.model.UserException

class GrillaPrincipal extends SimpleWindow<Jugador> {

	new(WindowOwner parent) {
		super(parent, new Jugador)
	}
	
	override def createMainTemplate(Panel mainPanel) {
		super.createMainTemplate(mainPanel)
		//this.createFormPanel(mainPanel)
		this.createJardinGrid(mainPanel)
			
	}
	

	override protected createFormPanel(Panel mainPanel){
	val form = new Panel(mainPanel)
		form.layout = new ColumnLayout(2)
		
	new Label(form).text = "Recursos:"
		val labelNombre = new Label(form)
		labelNombre.setWidth(150)
		labelNombre.bindValueToProperty("monedero")
	

}
	
	override protected addActions(Panel actionsPanel) {
		var plantar= new Button(actionsPanel)
			.setCaption("Plantar")
			.onClick [| this.plantar] 
			.setAsDefault
			.disableOnError

		var atacar = new Button(actionsPanel) //
			.setCaption("Atacar")
			.onClick [|this.atacar  ]

		new Button(actionsPanel) //
			.setCaption("Zombies")
			.onClick [| throw new UserException("nada") ]
			//.onClick [| this.VerZombies ]
		
		new Button(actionsPanel) //
			.setCaption("Jardin Zen")
			.onClick [ | this.jardinZen]
	
		var elementSelected = new NotNullObservable("filaSeleccionada")
		atacar.bindEnabled(elementSelected)
		plantar.bindEnabled(elementSelected)
		
			
	}
	
	def VerZombies() {
		this.openDialog(new CatalogoDeZombies(this, modelObject))
	}
	
	def atacar() {
		this.openDialog(new AtacarWindow(this, modelObject))
	}
	
	def plantar(){
		this.openDialog(new PlantarWindow(this, modelObject))
	}
	
	def jardinZen() {
		this.openDialog(new JardinZenWindow(this, modelObject))
	}
	
	def openDialog(Dialog<?> dialog) {
		dialog.open
	}
	
	
	def protected createJardinGrid(Panel mainPanel) {
		var table = new Table<Terreno>(mainPanel, typeof(Terreno))
		table.heigth = 200
		table.width = 450
		table.bindItemsToProperty("jardin.filas")
		table.bindValueToProperty("filaSeleccionada")
		this.describeJardinGrid(table)
	}
	def void describeJardinGrid(Table<Terreno> table) {
		new Column<Terreno>(table) //
			.setTitle("Tipo")
			.setFixedSize(150)
			.bindContentsToProperty("nombre")

		new Column<Terreno>(table) //
			.setTitle("Plantas")
			.setFixedSize(500)
			.bindContentsToProperty("fila")
	}
	
}